import datetime
import numpy as np
import pandas as pd
from scipy.fftpack import fft
from scipy.stats import skew, kurtosis


def get_activity_data():
    # activity_df01 = pd.read_csv('external_data/activity_df01.csv')
    # activity_df01.shop_id.replace('9M7G', '9M79', inplace=True)
    # activity_df02 = pd.read_csv('external_data/activity_df02.csv')
    # activity_df_lst = []
    # for name, group in activity_df01.groupby('shop_id'):
    #     group = pd.concat([group,activity_df02],ignore_index = True)
    #     group_df = group[[ 'start_date', 'end_date' ]]
    #     group_lst = []
    #     for i in range(len(group_df)):
    #         temp = pd.date_range(start = group_df.iloc[i]['start_date'],end = group_df.iloc[i]['end_date'])
    #         for ti in temp:
    #             time_s = int(ti.strftime('%Y%m%d'))
    #             if time_s not in group_lst:
    #                 group_lst.append(time_s)
    #     activity_group = pd.DataFrame(group_lst, columns=['date'])
    #     activity_group['shop_id'] = name
    #     activity_group['activity'] = 1
    #     activity_df_lst.append(activity_group)
    # activity_df = pd.concat(activity_df_lst,ignore_index = True)
    activity_df = pd.read_csv('active_data/activity_shop.csv')
    return activity_df

def divide_range(start, end, segments):
    interval_length = (end - start) / (segments+1)
    return [start + i * interval_length for i in range(1,segments+1)]

def get_festival_order(temp_df):
    temp_df['festival_order'] = list(range(1, len(temp_df) + 1))
    temp_df.holiday_today_cn = temp_df.holiday_today_cn.fillna(1)
    return temp_df

def get_holidays_data():
    holidays_df = pd.read_csv('external_data/holidays_df.csv')
    holidays_df.ds = holidays_df.ds.map(lambda x: x[:10])
    holiday_influence = pd.read_csv('external_data/holiday_influence.csv')
    holidays_df = pd.merge(holidays_df,holiday_influence,how ="left", on= ['holiday'])

    holidays_lst = []
    for i in range(len(holidays_df)):
        holiday = holidays_df.iloc[i].holiday
        hinfluence = holidays_df.iloc[i].holiday_influence
        holiday_id = holidays_df.iloc[i].holiday_id
        ds = datetime.datetime.strptime( holidays_df.iloc[i].ds,'%Y-%m-%d')
        start = (ds + datetime.timedelta(days = int(holidays_df.iloc[i].lower_window))).strftime('%Y-%m-%d')
        end =  (ds + datetime.timedelta(days = int(holidays_df.iloc[i].upper_window))).strftime('%Y-%m-%d')
        temp = pd.date_range(start = start,end = end)
        for i in range(len(temp)):
            time_int = int(temp[i].strftime('%Y%m%d'))
            holidays_lst.append([time_int,holiday, holiday_id,hinfluence])
    ret_df = pd.DataFrame(holidays_lst,columns=['date','holiday', 'holiday_id','hinfluence'])
    ret_df = ret_df.sort_values(by='holiday', ascending=True)
    ret_df = ret_df.reset_index(drop=True)
    ret_df = ret_df.drop_duplicates(['date'],keep='first')

    holiday_organized = pd.read_csv('external_data/holiday_organized.csv')
    holiday_organized = holiday_organized[['date', 'holiday_today_cn']]
    holid_dict = {'节日当天':2, '非节日当天':1}
    holiday_organized.holiday_today_cn =  holiday_organized.holiday_today_cn.map(lambda x: holid_dict[x])
    ret_df = pd.merge(ret_df, holiday_organized, how="left", on=['date'])

    ret_df = ret_df.sort_values(by=['date'],ascending=[True])
    ret_df = ret_df.reset_index(drop=True)
    subsets_df_lst = []
    sub_set = [0]
    for i in range(1, len(ret_df)):
        if ret_df.holiday.loc[i] == ret_df.holiday.loc[i-1]:
            sub_set.append(i)
        else:
            temp_df = ret_df.loc[sub_set,:].copy()
            temp_df = get_festival_order(temp_df)
            subsets_df_lst.append(temp_df)
            sub_set = [i]
    if len(sub_set) > 0:
        temp_df = ret_df.loc[sub_set, :].copy()
        temp_df = get_festival_order(temp_df)
        subsets_df_lst.append(temp_df)
    ret_df = pd.concat(subsets_df_lst,ignore_index = True)

    return ret_df


def get_workdays_data():
    workdays_df = pd.read_csv('external_data/workdays_df.csv')
    del workdays_df['inserttime']
    return workdays_df


def get_actual_data():
    actual_df = pd.read_csv('active_data/actual_df42.csv')
    actual_df.date_id = actual_df.date_id.astype(str)
    actual_df = actual_df[actual_df.date_id >= '2022-01-01']
    actual_df = actual_df[actual_df.date_id.map(lambda x: len(x)) >= 8]
    actual_df.shop_id.replace('9M7G', '9M79', inplace=True)
    actual_df.date_id = actual_df.date_id.map(lambda x: x[:10])

    actual_lst = []
    actual_days_lst = []
    for name, group in actual_df.groupby(['shop_id','date_id']):
        group01 = group[group.sdt == group.sdt.max()]
        actual_lst.append(group01)
        actual_days_lst.append([ name[0], name[1], group01.custflow.sum()])
    actual_df = pd.concat(actual_lst,ignore_index = True)
    actual_df = actual_df.drop('sdt',axis=1)
    actual_days_df = pd.DataFrame(actual_days_lst,columns=['shop_id','date','custflows'])
    return actual_df,actual_days_df


def get_forecast_data():
    forecast_df = pd.read_csv('active_data/forecast_df42.csv')
    forecast_df.date_id = forecast_df.date_id.map(lambda x: x[:10])
    forecast_df.shop_id.replace('9M7G', '9M79', inplace=True)
    forecast_lst = []
    forecast_days_lst = []
    for name, group in forecast_df.groupby(['shop_id','date_id']):
        date = datetime.datetime.strptime(str(name[1]), '%Y-%m-%d')
        last_wednesday = date - datetime.timedelta(days=date.weekday() + 1)
        last_wednesday -= datetime.timedelta(days=4)
        group01 = group[group.sdt == int(last_wednesday.strftime('%Y%m%d'))]
        if len(group01) > 0:
            forecast_lst.append(group01)
            forecast_days_lst.append([name[0], name[1], group01.custflow.sum()])
    forecast_df = pd.concat(forecast_lst,ignore_index = True)
    forecast_df = forecast_df.rename(columns={'custflow': 'pcustflow'})
    forecast_df = forecast_df.drop('sdt',axis=1)
    forecast_days_df = pd.DataFrame(forecast_days_lst,columns=['shop_id','date','pcustflows'])
    return forecast_df,forecast_days_df

def get_weather_data():
    weather_day_map = pd.read_csv('external_data/weather_day_map.csv')
    weather_night_map = pd.read_csv('external_data/weather_night_map.csv')

    weather_df = pd.read_csv('active_data/weather_df.csv')
    weather_df['date']= weather_df.date_id.map(lambda x: int(x.replace('-','')))
    weather_df = weather_df.drop('date_id', axis=1)

    weather_df = pd.merge(weather_df, weather_day_map, how="left", on=['day_rain'])
    weather_df = weather_df.drop('day_rain', axis=1)
    weather_df = weather_df.drop('day_weather_cata', axis=1)

    weather_df = pd.merge(weather_df, weather_night_map, how="left", on=['night_rain'])
    weather_df = weather_df.drop('night_rain', axis=1)
    weather_df = weather_df.drop('night_weather_cata', axis=1)

    weather_df = weather_df[['shop_id', 'date', 'day_weather_level', 'night_weather_level']]

    return  weather_df



